package com.hulu.yec.sys.yecclass.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hulu.yec.common.Constant;
import com.hulu.yec.sys.yecclass.entity.ClassEntity;
import com.hulu.yec.sys.yecclass.service.ClassAddDao;
import com.hulu.yec.sys.yecclass.service.ClassDao;
import com.hulu.yec.sys.yecclass.service.ClassDeleteDao;
import com.hulu.yec.sys.yecclass.service.TermDao;

@WebServlet("/yecclass/ClassServlet")
public class ClassServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private String sql;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding(Constant.ENCODING_UTF8);

		String command = request.getParameter("command");

		if ("list".equals(command)) {
			list(request, response);
		} else if ("updateback".equals(command)) {
			updateback(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("classdelete".equals(command)) {
			classdelete(request, response);
		} else if ("updateState".equals(command)) {
			updateState(request, response);
		} else if ("updateclassback".equals(command)) {
			updateclassback(request, response);
		} else if ("ClassAdd".equals(command)) {
			ClassAdd(request, response);
		} else if ("onlyclassname".equals(command)) {
			onlyclassname(request, response);
		} else if ("get".equals(command)) {
			get(request, response);
		} else {
			throw new ServletException("servlet请求错误！");
		}
	}

	/**
	 * 条件查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取前台参数
			String coursename = request.getParameter("coursename");
			String createtimeup = request.getParameter("createtimeup");
			String createtimedown = request.getParameter("createtimedown");
			String IfError = "不存在你查询的数据~~~";
			// 调用业务逻辑
			List<ClassEntity> listUser = TermDao.IfList(coursename,
					createtimeup, createtimedown);

			request.setAttribute("coursename", coursename);
			request.setAttribute("createtimeup", createtimeup);
			request.setAttribute("createtimedown", createtimedown);
			request.setAttribute("listUser", listUser);
			// 转向
			if (listUser.size() != 0) {
				request.getRequestDispatcher(
						"/WEB-INF/view/sys/class/class_list.jsp").forward(
						request, response);
			} else {
				request.setAttribute("IfError", IfError);
				request.getRequestDispatcher(
						"/WEB-INF/view/sys/class/class_list.jsp").forward(
						request, response);
			}
		} catch (SQLException e) {
			throw new ServletException("请求失败！");
		}

	}

	/**
	 * 班级新增的班级名唯一验证
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void onlyclassname(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			String name = request.getParameter("name");
			String sql = "select id FROM sys_class WHERE name='" + name + "'";

			ClassAddDao.ListId(sql, name, request, response);

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 班级新增
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void ClassAdd(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		try {
			// 调用前台参数
			String name = request.getParameter("name");
			String course_name = request.getParameter("course_name");
			String memo = request.getParameter("memo");

			// 调用业务逻辑
			String sql = "INSERT INTO sys_class VALUES(NULL,'" + name + "',"
					+ course_name + ", 5, NOW(), 1, '" + memo + "')";

			int row = ClassDeleteDao.states(sql);

			if (row == 1) {
				// 转向
				request.getRequestDispatcher(
						"/WEB-INF/view/sys/class/update-run.jsp").forward(
						request, response);
			} else {

				request.getRequestDispatcher(
						"/WEB-INF/view/sys/class/class-add.jsp").forward(
						request, response);
			}

		} catch (SQLException e) {
			throw new RuntimeException("简历查询失败~");
		}
	}

	/**
	 * 班级新增课程回显
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void updateclassback(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {

			String sqll = "SELECT id, name as 'course_name' FROM sys_course";
			// 调用业务逻辑
			List<ClassEntity> listcourse = ClassDao.listl(sqll);
			// 转向
			request.setAttribute("listcourse", listcourse);
			request.getRequestDispatcher(
					"/WEB-INF/view/sys/class/class-add.jsp").forward(request,
					response);
		} catch (SQLException e) {
			throw new ServletException("Servlet请求错误！");
		}

	}

	/**
	 * STATE的修改
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updateState(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// 调用前台参数
			int id = Integer.parseInt(request.getParameter("id"));

			int state = Integer.parseInt(request.getParameter("state"));
			if (state == 1) {
				sql = "UPDATE sys_class SET state =2  WHERE id =" + id;

			} else {
				sql = "UPDATE sys_class SET state =1  WHERE id =" + id;
			}
			// 调用业务逻辑
			int row = ClassAddDao.states(sql);

			if (row == 1) {
				request.setAttribute("row", row);
				request.getRequestDispatcher(
						"/yecclass/ClassServlet?command=list").forward(request,
						response);
			} else {
				return;
			}
		} catch (SQLException e) {
			throw new ServletException("Servlet跳转错误！");
		}
	}

	/**
	 * 班级管理删除信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void classdelete(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		try {

			// 调用前台参数
			int id = Integer.parseInt(request.getParameter("id"));
			// 调用业务逻辑
			String sql = "DELETE FROM sys_class  WHERE  id= " + id;

			int row = ClassDeleteDao.states(sql);

			// 转向
			request.getRequestDispatcher("/yecclass/ClassServlet?command=list")
					.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException("请求Servlet错误！");
		}
	}

	/**
	 * 修改班级信息回显
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void updateback(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// 调用前台参数
			int id = Integer.parseInt(request.getParameter("id"));
			String sql = "SELECT id,  name, operate_time, state, memo FROM sys_class WHERE id ="
					+ id;
			String sqll = "SELECT id, name as 'course_name' FROM sys_course";
			// 调用业务逻辑
			List<ClassEntity> listUser = ClassDao.lists(sql);
			List<ClassEntity> listcourse = ClassDao.listl(sqll);

			request.setAttribute("listUser", listUser);
			request.setAttribute("listcourse", listcourse);

			request.getRequestDispatcher(
					"/WEB-INF/view/sys/class/class_update.jsp").forward(
					request, response);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new ServletException("Servlet请求错误！");
		}

	}

	/**
	 * 班级信息修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			// 调用前台参数
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name");
			String course_name = request.getParameter("course_name");
			int state = Integer.parseInt(request.getParameter("state"));
			String memo = request.getParameter("memo");

			String sql = "UPDATE sys_class c, sys_course p SET c.name='" + name
					+ "' , p.name='" + course_name + "' , c.state=" + state
					+ " , c.memo='" + memo + "' WHERE p.id=course_id AND c.id="
					+ id;

			// 调用业务
			int row = ClassDao.states(sql);

			if (row == 2 || row == 1) {
				request.setAttribute("success", "操作成功！");
				request.getRequestDispatcher(
						"/WEB-INF/view/sys/class/update-run.jsp").forward(
						request, response);
			} else {
				request.setAttribute("error", "操作失败！");
				return;
			}
		} catch (SQLException e) {
			throw new RuntimeException("请求数据库失败");
		}

	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String sql = "SELECT c.id, c.name, p.name as 'course_name', c.operate_time, c.state, c.memo FROM sys_class c, sys_course p WHERE p.id=c.course_id ORDER BY c.id DESC";

			List<ClassEntity> listUser = ClassDao.list(sql);

			request.setAttribute("listUser", listUser);
			request.getRequestDispatcher(
					"/WEB-INF/view/sys/class/class_list.jsp").forward(request,
					response);
		} catch (SQLException e) {
			throw new RuntimeException("数据库驱动加载错误！");
		}
	}

}
